#pragma once

#include <iostream>
#include <cstring>
#include <cerrno>
#include <cstdlib>
#include <string>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <jsoncpp/json/json.h>
using namespace std;

// 请求结构
typedef struct Request
{
    /* data */
    int _x;
    int _y;
    char _op;
} request_t;

// 响应结构
typedef struct Response
{
    /* data */
    int _code;
    int _result;
} response_t;

// request_serialization
string request_serialization(request_t &req)
{
    Json::Value root;
    root["_x"] = req._x;
    root["_y"] = req._y;
    root["_op"] = req._op;

    Json::FastWriter writer;
    string Json_string = writer.write(root);
    return Json_string;
}

// request_Deserialization
void request_deserialization(const string &json_string, request_t &out)
{
    Json::Value root;
    Json::Reader reader;

    reader.parse(json_string, root);
    out._x = root["_x"].asInt();
    out._y = root["_y"].asInt();
    out._op = (char)root["_op"].asInt();
}

// response_serialization
string response_serialization(response_t &resp)
{
    Json::Value root;
    root["_code"] = resp._code;
    root["_result"] = resp._result;

    Json::FastWriter writer;
    string json_string = writer.write(root);
    return json_string;
}

// response_Deserialization
void response_deserialization(const string &json_string, response_t &out)
{
    Json::Value root;
    Json::Reader reader;

    reader.parse(json_string, root);
    out._code = root["_code"].asInt();
    out._result = root["_result"].asInt();
}